<?php

namespace app\service;

use share\service\cache\CommonCacheKeyService;
use share\service\RedisService;
use share\utils\Helper;

class UrlConfigService {

    CONST CLIENT_TYPE_OF_ANDROID = 1;
    CONST CLIENT_TYPE_OF_IOS = 2;
    CONST CLIENT_TYPE_OF_DEVICE = 3;
    CONST CLIENT_TYPE_OF_WEB = 4;
    CONST CLIENT_TYPE_OF_CMS = 5;
    CONST CLIENT_TYPE_OF_OTHER = 99;

    CONST SELECTOR_TYPE_OF_IP = 'ip';
    CONST SELECTOR_TYPE_OF_VENDOR = 'vendor';
    CONST SELECTOR_TYPE_OF_PACKAGE = 'package';
    CONST SELECTOR_TYPE_OF_VERSION = 'version';
    CONST SELECTOR_TYPE_OF_CLIENT = 'client';
    const SELECTOR_TYPE_OF_CLIENT_TYPE = 'client_type';
    CONST SELECTOR_TYPE_OF_DID = 'did';

    CONST DOMAIN_TYPE_OF_ACCOUNT = 'account';
    CONST DOMAIN_TYPE_OF_OAUTH = 'oauth';
    CONST DOMAIN_TYPE_OF_BURNSUPER = 'burn';
    CONST DOMAIN_TYPE_OF_SIM = 'sim';
    CONST DOMAIN_TYPE_OF_OSS = 'oss';
    CONST DOMAIN_TYPE_OF_H5 = 'h5';
    CONST DOMAIN_TYPE_OF_APP_SHARE = 'appShare';


    CONST URL_CONFIG_CACHE_TIMEOUT = 86400; // 过期 一天

    public static function getAllConfig(){
        $urlData = self::getAllByCache();
        // debug
        // $urlData = self::getAllRawData();
        return $urlData;
    }

    public static function getAllByCache(){
        $cacheKey = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_URL_CONFIG);
        $data = RedisService::instance(RedisService::CACHE_DB_BASE)->getJson($cacheKey);
        if(!$data){
            $data = self::getAllRawData();
            if($data){
                RedisService::instance(RedisService::CACHE_DB_BASE)->setJson($cacheKey,$data,self::URL_CONFIG_CACHE_TIMEOUT);
            }
        }
        return $data;
    }

    /**
     * 删除缓存
     */
    public static function deleteCache(){
        $cacheKey = CommonCacheKeyService::getCacheKey(CommonCacheKeyService::CACHE_KEY_URL_CONFIG);
        RedisService::instance(RedisService::CACHE_DB_BASE)->del($cacheKey);
    }

    public static function getAllRawData(){
        $domain = UrlDomainService::getNativeAll(null,['id','domain','type','is_default']);
        $relation = UrlDomainRelationService::getNativeAll(null,['url_domain_id','domain_type','url_selector_id','url_selector_type','priority'],'priority DESC');
        $selector = UrlSelectorService::getNativeAll(null,['id','type','value','group_id']);
        return [
            'domain' => $domain,
            'relation' => $relation,
            'selector' => $selector,
        ];      
    }

    public static function getClientTypeByName($type){
        $type = strtoupper(trim($type));
        if($type == 'ANDROID'){
            return self::CLIENT_TYPE_OF_ANDROID;
        }
        else if($type == 'IOS'){
            return self::CLIENT_TYPE_OF_IOS;
        }
        else if($type == 'IPCAM' || $type == 'NVR' || $type == 'IVR' ){
            return self::CLIENT_TYPE_OF_DEVICE;
        }
        else if($type == 'BROWSER'){
            return self::CLIENT_TYPE_OF_WEB;
        }
        else if($type == 'CMS'){
            return self::CLIENT_TYPE_OF_CMS;
        }
        else{
            return  self::CLIENT_TYPE_OF_OTHER;
        }
    }

    public static function getDescDomainTypeList(){
        return [
            self::DOMAIN_TYPE_OF_SIM => '4G流量服务平台',
            self::DOMAIN_TYPE_OF_OSS => '云存API',
            self::DOMAIN_TYPE_OF_ACCOUNT => '账号服务',
            self::DOMAIN_TYPE_OF_OAUTH => '授权',
            self::DOMAIN_TYPE_OF_BURNSUPER => '烧录',
            self::DOMAIN_TYPE_OF_H5 => '云存购买',
            self::DOMAIN_TYPE_OF_APP_SHARE => 'App好友分享',
        ];
    }

    public static function getDomainTypeIndex(){
        return [
            self::DOMAIN_TYPE_OF_ACCOUNT => 1,
            self::DOMAIN_TYPE_OF_OAUTH => 2,
            self::DOMAIN_TYPE_OF_BURNSUPER => 3,
            self::DOMAIN_TYPE_OF_SIM => 4,
            self::DOMAIN_TYPE_OF_H5 => 5,
            self::DOMAIN_TYPE_OF_OSS => 6,
            self::DOMAIN_TYPE_OF_APP_SHARE => 7,
        ];
    }

    public static function getDomainTypeNameByIndex($index){
        $data = self::getDomainTypeIndex();
        $ret = array_search($index,$data);
        if($ret !== false){
            return $ret;
        }
        return "";
    }

    public static function getDomainTypeIndexByName($name){
        $data = self::getDomainTypeIndex();
        return isset($data[$name]) ? $data[$name] : 0;
    }

    public static function getDescClientTypeList(){
        return [
            self::CLIENT_TYPE_OF_ANDROID => '安卓',
            self::CLIENT_TYPE_OF_IOS => '苹果',
            self::CLIENT_TYPE_OF_DEVICE => '设备端',
            self::CLIENT_TYPE_OF_WEB => 'web端',
            self::CLIENT_TYPE_OF_CMS => 'CMS',
            self::CLIENT_TYPE_OF_OTHER => '其他',
        ];
    }

    public static function getDescSelectorTypeList(){
        return [
            self::SELECTOR_TYPE_OF_IP => 'ip地区',
            self::SELECTOR_TYPE_OF_VENDOR => '厂商',
            self::SELECTOR_TYPE_OF_PACKAGE => '应用',
            self::SELECTOR_TYPE_OF_VERSION => '应用版本',
            self::SELECTOR_TYPE_OF_CLIENT_TYPE => '终端类型',
            self::SELECTOR_TYPE_OF_CLIENT => '终端',
            self::SELECTOR_TYPE_OF_DID => '设备id',
        ];
    }
}